#import "GKCover.h"
#import "dfhgwhruebvhrheyadmitultimate.h"
#import "dfhgwhruebvhrheyoutwarddelegate.h"
#import "dfhgwhruebvhrheyluggagerecorder.h"
#import "dfhgwhruebvhrheyprofessionalincidentally.h"
#import "dfhgwhruebvhrheyairplaneflock.h"
#import "dfhgwhruebvhrheycataloguetheory.h"
#import "dfhgwhruebvhrheyprevalentconnectionViewController.h"
#import "dfhgwhruebvhrheynastydispositionViewController.h"
#import "dfhgwhruebvhrheycandidateroutineViewController.h"
#import "dfhgwhruebvhrheyarrestmeans.h"
#import "dfhgwhruebvhrheysenatorhunt.h"
#import "dfhgwhruebvhrheywarrantfeasibleViewController.h"
#import "dfhgwhruebvhrheydefinitelyleakViewController.h"
#import "dfhgwhruebvhrheyminimalfantasyViewController.h"
#import "dfhgwhruebvhrheyvisualizeterritoryViewController.h"
#import "dfhgwhruebvhrheylabelfocus.h"
#import "dfhgwhruebvhrheynamelyprevalentViewController.h"
@interface dfhgwhruebvhrheynamelyprevalentViewController ()
@property (nonatomic, assign) bool isRepayment;
@property (nonatomic, strong) dfhgwhruebvhrheyprofessionalincidentally *useModel;
@property (nonatomic, strong) dfhgwhruebvhrheyprofessionalincidentally *sysModel;
@property (nonatomic, strong) dfhgwhruebvhrheyairplaneflock *couponView;
@property (nonatomic, strong) dfhgwhruebvhrheysenatorhunt *statusModel;
@property (nonatomic, strong) dispatch_group_t group;
@property (weak, nonatomic) IBOutlet UIButton *submitBtn;
@property (weak, nonatomic) IBOutlet UIButton *jobStatusBtn;
@property (weak, nonatomic) IBOutlet UIButton *basicStatusBtn;
@property (weak, nonatomic) IBOutlet UIButton *identityStatusBtn;
@property (weak, nonatomic) IBOutlet UIButton *emergencyStatusBtn;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *jobStatusBtnWidth;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *basicStatusBtnWidth;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *identityStatusBtnWidth;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *emergencyStatusBtnWidth;
@end
@implementation dfhgwhruebvhrheynamelyprevalentViewController
- (dfhgwhruebvhrheyairplaneflock *)couponView {
	if (_couponView == nil) {
		_couponView = [[NSBundle mainBundle] loadNibNamed:@"dfhgwhruebvhrheyairplaneflock" owner:nil options:nil].firstObject;
	}
	return _couponView;
}
- (void)viewDidLoad {
	[super viewDidLoad];
	[self loadUI];
	[self loadData];
	NSString *identifier = [[NSUserDefaults standardUserDefaults] stringForKey:[NSString stringWithFormat:@"%@_%@", [dfhgwhruebvhrheyoptionsaving getUserData].phone, dhgfywegwygeydwconnectionencounter]];
	if ([identifier isEqualToString:@"identity"]) {
		[[NSUserDefaults standardUserDefaults] setObject:@"authentication" forKey:[NSString stringWithFormat:@"%@_%@", [dfhgwhruebvhrheyoptionsaving getUserData].phone, dhgfywegwygeydwconnectionencounter]];
	}
}
- (void)loadData {
	__weak typeof(self) weakSelf = self;
	if (self.group == nil) {	
		self.group = dispatch_group_create();
	}
	dispatch_group_enter(self.group);
	[self loadStatus];
	dispatch_group_enter(self.group);
	[self systemCoupinList];
	dispatch_group_enter(self.group);
	[self getUserCouponList];
	[dfhgwhruebvhrheyoptionsaving.keyWindow startLoading];
	dispatch_group_notify(self.group, dispatch_get_main_queue(), ^{
		__strong typeof(weakSelf) strongSelf = weakSelf;
		[dfhgwhruebvhrheyoptionsaving.keyWindow stopLoading];
		[strongSelf bindAuthStatusData];
		[strongSelf bindCouponList];
	});
}
- (void)getUserCouponList {
	__weak typeof(self) weakSelf = self;
	[[dfhgwhruebvhrheyluggagerecorder shareInstance] dhgfywegwygeydwbarrierawait:dhgfywegwygeydwlambbreast path:userCouponListApi isEncrypt:YES withParams:nil success:^(NSURLSessionDataTask * _Nonnull task, NSString * _Nonnull json) {
		__strong typeof(weakSelf) strongSelf = weakSelf;
		strongSelf.useModel = [dfhgwhruebvhrheyprofessionalincidentally yy_modelWithJSON:json];
		dispatch_group_leave(strongSelf.group);
	} failure:^(NSURLSessionDataTask * _Nonnull task, NSError * _Nonnull error) {
		__strong typeof(weakSelf) strongSelf = weakSelf;
		dispatch_group_leave(strongSelf.group);
	}];
}
- (void)systemCoupinList {
	__weak typeof(self) weakSelf = self;
	[[dfhgwhruebvhrheyluggagerecorder shareInstance] dhgfywegwygeydwbarrierawait:dhgfywegwygeydwlambbreast path:systemCouponListApi isEncrypt:YES withParams:nil success:^(NSURLSessionDataTask * _Nonnull task, NSString * _Nonnull json) {
		__strong typeof(weakSelf) strongSelf = weakSelf;
		strongSelf.sysModel = [dfhgwhruebvhrheyprofessionalincidentally yy_modelWithJSON:json];
		dispatch_group_leave(strongSelf.group);
	} failure:^(NSURLSessionDataTask * _Nonnull task, NSError * _Nonnull error) {
		__strong typeof(weakSelf) strongSelf = weakSelf;
		dispatch_group_leave(strongSelf.group);
	}];
}
- (void)loadStatus {
	__weak typeof(self) weakSelf = self;
	[[dfhgwhruebvhrheyluggagerecorder shareInstance] dhgfywegwygeydwbarrierawait:dhgfywegwygeydwlambbreast path:authCenterStatusApi isEncrypt:YES withParams:nil success:^(NSURLSessionDataTask * _Nonnull task, NSString * _Nonnull json) {
		__strong typeof(weakSelf) strongSelf = weakSelf;
		strongSelf.statusModel = [dfhgwhruebvhrheysenatorhunt yy_modelWithJSON:json];
		dispatch_group_leave(strongSelf.group);
	} failure:^(NSURLSessionDataTask * _Nonnull task, NSError * _Nonnull error) {
		__strong typeof(weakSelf) strongSelf = weakSelf;
		dispatch_group_leave(strongSelf.group);
	}];
}
- (void)bindAuthStatusData {
	if (self.statusModel.code == 200) {
		if (self.statusModel.is_identity_state == 1) {
			self.basicStatusBtn.selected = YES;
			self.basicStatusBtnWidth.constant = 110;
			self.basicStatusBtn.backgroundColor = primary_color.dhgfywegwygeydwlearneddesignate;
			[self.basicStatusBtn setTitle:@"Completed" forState:UIControlStateNormal];
			self.jobStatusBtn.selected = YES;
			self.jobStatusBtnWidth.constant = 110;
			self.jobStatusBtn.backgroundColor = primary_color.dhgfywegwygeydwlearneddesignate;
			[self.jobStatusBtn setTitle:@"Completed" forState:UIControlStateNormal];
			self.emergencyStatusBtn.selected = YES;
			self.emergencyStatusBtnWidth.constant = 110;
			self.emergencyStatusBtn.backgroundColor = primary_color.dhgfywegwygeydwlearneddesignate;
			[self.emergencyStatusBtn setTitle:@"Completed" forState:UIControlStateNormal];
		}else {
			NSInteger progress = [[NSUserDefaults standardUserDefaults] integerForKey:[NSString stringWithFormat:@"%@_%@", [dfhgwhruebvhrheyoptionsaving getUserData].phone, dhgfywegwygeydwproposediagram]];
			if (progress == 1) {
				self.basicStatusBtn.selected = YES;
				self.basicStatusBtnWidth.constant = 110;
				self.basicStatusBtn.backgroundColor = primary_color.dhgfywegwygeydwlearneddesignate;
				[self.basicStatusBtn setTitle:@"Completed" forState:UIControlStateNormal];
			}
			if (progress == 2) {
				self.basicStatusBtn.selected = YES;
				self.basicStatusBtnWidth.constant = 110;
				self.basicStatusBtn.backgroundColor = primary_color.dhgfywegwygeydwlearneddesignate;
				[self.basicStatusBtn setTitle:@"Completed" forState:UIControlStateNormal];
				self.jobStatusBtn.selected = YES;
				self.jobStatusBtnWidth.constant = 110;
				self.jobStatusBtn.backgroundColor = primary_color.dhgfywegwygeydwlearneddesignate;
				[self.jobStatusBtn setTitle:@"Completed" forState:UIControlStateNormal];
			}
			if (progress == 3) {
				self.basicStatusBtn.selected = YES;
				self.basicStatusBtnWidth.constant = 110;
				self.basicStatusBtn.backgroundColor = primary_color.dhgfywegwygeydwlearneddesignate;
				[self.basicStatusBtn setTitle:@"Completed" forState:UIControlStateNormal];
				self.jobStatusBtn.selected = YES;
				self.jobStatusBtnWidth.constant = 110;
				self.jobStatusBtn.backgroundColor = primary_color.dhgfywegwygeydwlearneddesignate;
				[self.jobStatusBtn setTitle:@"Completed" forState:UIControlStateNormal];
				self.emergencyStatusBtn.selected = YES;
				self.emergencyStatusBtnWidth.constant = 110;
				self.emergencyStatusBtn.backgroundColor = primary_color.dhgfywegwygeydwlearneddesignate;
				[self.emergencyStatusBtn setTitle:@"Completed" forState:UIControlStateNormal];
			}
		}
		if (self.statusModel.is_face_state == 1) {
			self.identityStatusBtn.selected = YES;
			self.identityStatusBtnWidth.constant = 110;
			self.identityStatusBtn.backgroundColor = primary_color.dhgfywegwygeydwlearneddesignate;
			[self.identityStatusBtn setTitle:@"Completed" forState:UIControlStateNormal];
		}
		if (!(self.statusModel.is_examine == 1 && self.statusModel.is_face_state == 1 && self.statusModel.is_identity_state == 1)) {
			self.submitBtn.hidden = NO;
		}
	}else {
		[dfhgwhruebvhrheyoptionsaving.keyWindow show:self.statusModel.error];
	}
}
- (void)bindCouponList {
	if (self.useModel.code != 200) {
		[dfhgwhruebvhrheyoptionsaving.keyWindow show:self.useModel.error];
	}
	if (self.sysModel.code != 200) {
		[dfhgwhruebvhrheyoptionsaving.keyWindow show:self.sysModel.error];
	}
	__weak typeof(self) weakSelf = self;
	dfhgwhruebvhrheycandidateroutineViewController *tabBarController = (dfhgwhruebvhrheycandidateroutineViewController *)dfhgwhruebvhrheyoptionsaving.keyWindow.rootViewController;
	dfhgwhruebvhrheynastydispositionViewController *home = (dfhgwhruebvhrheynastydispositionViewController *)((dfhgwhruebvhrheyarrestmeans *)tabBarController.viewControllers.firstObject).viewControllers.firstObject;
	self.isRepayment = (home.homeModel.status == 5 && home.homeModel.user_loan_status == 0);
	if (home.showPopData.is_open_coupon == 1 && ((self.useModel.data && self.useModel.data.count) || (self.sysModel.data && self.sysModel.data.count))) {
		[GKCover coverFrom:[dfhgwhruebvhrheyoptionsaving keyWindow] contentView:self.couponView style:GKCoverStyleTranslucent showStyle:GKCoverShowStyleCenter showAnimStyle:GKCoverShowAnimStyleCenter hideAnimStyle:GKCoverHideAnimStyleCenter notClick:NO];
		[self.couponView couponViewType:PPP_CouponPopViewTypeAuthList andClick:^{
			__strong typeof(weakSelf) strongSelf = weakSelf;
			[GKCover hideCover];
			[strongSelf couponViewClick];
		}];
	}
}
- (void)couponViewClick {
	dfhgwhruebvhrheyminimalfantasyViewController *list = (dfhgwhruebvhrheyminimalfantasyViewController *)[UIStoryboard storyboard:@"Home" withidentifier:@"coupon_list"];
	list.isRepayment = self.isRepayment;
	[self.navigationController pushViewController:list animated:YES];
}
- (void)loadUI {
	[self configBackIcon:@"dhgfywegwygeydwcurvereciprocal"];
	[self configRightIcon:@"navigation_right"];
	self.navigationStyle = dhgfywegwygeydwreciprocaltransport;
	self.navigationItem.title = @"Identity Authentication";
	self.submitBtn.hidden = YES;
	self.basicStatusBtnWidth.constant = 80;
	[self.basicStatusBtn setTitle:@"Verify" forState:UIControlStateNormal];
	self.jobStatusBtnWidth.constant = 80;
	[self.jobStatusBtn setTitle:@"Verify" forState:UIControlStateNormal];
	self.emergencyStatusBtnWidth.constant = 80;
	[self.emergencyStatusBtn setTitle:@"Verify" forState:UIControlStateNormal];
	self.identityStatusBtnWidth.constant = 80;
	[self.identityStatusBtn setTitle:@"Verify" forState:UIControlStateNormal];
    CAGradientLayer *gradientLayer = [CAGradientLayer layer];
    gradientLayer.frame = self.view.bounds;
    gradientLayer.startPoint = CGPointMake(0, 0);
    gradientLayer.endPoint = CGPointMake(0, 1);
    gradientLayer.colors = @[(__bridge  id)primary_color.dhgfywegwygeydwlearneddesignate.CGColor, (__bridge  id)second_color.dhgfywegwygeydwlearneddesignate.CGColor];
    gradientLayer.locations = @[@(0.2), @(1.0)];
    [self.view.layer insertSublayer:gradientLayer atIndex:0];
}
- (IBAction)authItemClick:(UITapGestureRecognizer *)sender {
	NSInteger progress = [[NSUserDefaults standardUserDefaults] integerForKey:[NSString stringWithFormat:@"%@_%@", [dfhgwhruebvhrheyoptionsaving getUserData].phone, dhgfywegwygeydwproposediagram]];
	switch (sender.view.tag) {
		case 100: {
			NSLog(@"基本信息");
			if (self.statusModel.is_identity_state == 1) {
				return [dfhgwhruebvhrheyoptionsaving.keyWindow show:@"Please note that the data cant't be changed after submission"];
			}
			dfhgwhruebvhrheydefinitelyleakViewController *basicinfo = (dfhgwhruebvhrheydefinitelyleakViewController *)[UIStoryboard storyboard:@"Home" withidentifier:@"basic_info"];
			basicinfo.hidesBottomBarWhenPushed = YES;
			[self.navigationController pushViewController:basicinfo animated:YES];
		}
			break;
		case 200: {
			NSLog(@"工作信息");
			if (self.statusModel.is_identity_state == 1) {
				return [dfhgwhruebvhrheyoptionsaving.keyWindow show:@"Please note that the data cant't be changed after submission"];
			}
			if (progress == 0) {
				return [dfhgwhruebvhrheyoptionsaving.keyWindow show:@"Please complete basic information"];
			}
			dfhgwhruebvhrheyprevalentconnectionViewController *job = (dfhgwhruebvhrheyprevalentconnectionViewController *)[UIStoryboard storyboard:@"Home" withidentifier:@"auth_job"];
			[self.navigationController pushViewController:job animated:YES];
		}
			break;
		case 300: {
			NSLog(@"紧急联系人");
			if (self.statusModel.is_identity_state == 1) {
				return [dfhgwhruebvhrheyoptionsaving.keyWindow show:@"Please note that the data cant't be changed after submission"];
			}
			if (progress == 0) {
				return [dfhgwhruebvhrheyoptionsaving.keyWindow show:@"Please complete basic information"];
			}
			if (progress == 1) {
				return [dfhgwhruebvhrheyoptionsaving.keyWindow show:@"Please complete job information"];
			}
			dfhgwhruebvhrheyvisualizeterritoryViewController *list = (dfhgwhruebvhrheyvisualizeterritoryViewController *)[UIStoryboard storyboard:@"Home" withidentifier:@"contact"];
			[self.navigationController pushViewController:list animated:YES];
		}
			break;
		case 400: {
			if (self.statusModel.is_identity_state != 1) {
				return [dfhgwhruebvhrheyoptionsaving.keyWindow show:@"Please complete basic information"];
			}
			if (self.statusModel.is_face_state == 1) {
				return [dfhgwhruebvhrheyoptionsaving.keyWindow show:@"Please note that the data cant't be changed after submission"];
			}
			dfhgwhruebvhrheywarrantfeasibleViewController *identity = (dfhgwhruebvhrheywarrantfeasibleViewController *)[UIStoryboard storyboard:@"Home" withidentifier:@"identity"];
			[self.navigationController pushViewController:identity animated:YES];
		}
			break;
		default:
			break;
	}
}
- (void)goHome {
	[self.navigationController popToRootViewControllerAnimated:YES];
	dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
		dfhgwhruebvhrheycandidateroutineViewController *tabBar = (dfhgwhruebvhrheycandidateroutineViewController *)[dfhgwhruebvhrheyoptionsaving.keyWindow rootViewController];
		tabBar.selectedIndex = 0;
	});
}
- (void)submitSuccess {
	__weak typeof(self) weakSelf = self;
	dfhgwhruebvhrheylabelfocus *successPopView = [[NSBundle mainBundle] loadNibNamed:@"dfhgwhruebvhrheylabelfocus" owner:nil options:nil].firstObject;
	[GKCover coverFrom:[dfhgwhruebvhrheyoptionsaving keyWindow] contentView:successPopView style:GKCoverStyleTranslucent showStyle:GKCoverShowStyleCenter showAnimStyle:GKCoverShowAnimStyleCenter hideAnimStyle:GKCoverHideAnimStyleCenter notClick:YES];
	[successPopView confirm:^(UIButton * _Nonnull sender) {
		__strong typeof(weakSelf) strongSelf = weakSelf;
		[GKCover hideCover];
		if (sender.tag == 100) {
			dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
				[strongSelf goHome];
			});
			NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"itms-apps://itunes.apple.com/app/id%@?action=write-review", APPID]];
			if([[UIApplication sharedApplication] canOpenURL:url]) {
				[[UIApplication sharedApplication] openURL:url options:@{} completionHandler:nil];
			}
		}else {
			[strongSelf backAction];
		}
	}];
}
- (IBAction)submitAction {
	if (self.statusModel.is_identity_state != 1) {
		return [dfhgwhruebvhrheyoptionsaving.keyWindow show:@"Please complete basic information"];
	}
	if (self.statusModel.is_face_state != 1) {
		return [dfhgwhruebvhrheyoptionsaving.keyWindow show:@"Please complete identity information"];
	}
	[dfhgwhruebvhrheyoptionsaving.keyWindow startLoading];
	__weak typeof(self) weakSelf = self;
	[[dfhgwhruebvhrheyluggagerecorder shareInstance] dhgfywegwygeydwbarrierawait:dhgfywegwygeydwawaitlamb path:identityCenterSubmitApi isEncrypt:YES withParams:nil success:^(NSURLSessionDataTask * _Nonnull task, NSString * _Nonnull json) {
		[dfhgwhruebvhrheyoptionsaving.keyWindow stopLoading];
		__strong typeof(weakSelf) strongSelf = weakSelf;
		dfhgwhruebvhrheysternideology *model = [dfhgwhruebvhrheysternideology yy_modelWithJSON:json];
		if (model.code == 200) {
			[strongSelf submitSuccess];
			[[NSUserDefaults standardUserDefaults] setObject:nil forKey:[NSString stringWithFormat:@"%@_%@", [dfhgwhruebvhrheyoptionsaving getUserData].phone, dhgfywegwygeydwconnectionencounter]];
			[dfhgwhruebvhrheyoptionsaving uploadRiskDataByCatchId:@"1" andApplyId:nil withDetailId:nil];
		}else {
			[dfhgwhruebvhrheyoptionsaving.keyWindow show:model.error];
		}
	} failure:^(NSURLSessionDataTask * _Nonnull task, NSError * _Nonnull error) {
		[dfhgwhruebvhrheyoptionsaving.keyWindow stopLoading];
	}];
}
- (void)backAction {
	[self.navigationController popToRootViewControllerAnimated:YES];
}
@end
